<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : integration.class.php   FILE_PATH : D:\webroot\_own\classes\application\integration.class.php
 * ....会员整合
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Mon Feb 25 14:19:04 CST 2008
 */

class Integration extends FrameWork {
	
	var $inte_db_server_name = "localhost";//数据库连接地址
	
	var $inte_db_account = "";//数据库帐号
	
	var $inte_db_password = "";//数据库密码
	
	var $inte_db_name = "";//数据库名
	
	var $inte_prefix = "";//前缀
	
	var $exits_array = "";

	var $is_clearTable = 1;
	
	var $intergration = "";//整合系统类别
	
	var $charset = "utf-8";
	
	var $_dbprefix = "";
	
	var $_conn;//数据库连接
	
	var $_conn_write;//数据库连接
	/**
	 * 设置导入系统前缀名称
	 *
	 */
	function setPrefix(){
		$this->_dbprefix = $this->inte_prefix;
	}

	/**
	 * 设置SHOP系统前缀名称
	 *
	 */
	function setShopPrefix(){
		$this->_dbprefix = $GLOBALS['dbconfig']['dbprefix'];
	}

	/**
	 * 如果填写数据库名称,更换数据库连接
	 */
	function setConnect(){
		return $this->connectConformityDB();
	}
	/**
	 * 转换回SHOP数据库
	 *
	 */
	function changeConnectBack(){
		return $GLOBALS['db']->DataBase();
	}

	/**
	 * 得到SHOP的会员信息列表
	 *
	 * @return string[]
	 */
	function getMemberList(){
		$this->setShopPrefix();
		$member_array = $GLOBALS['db']->GetList('',$page,'member');
		return $member_array;
	}

	/**
	 * 得到SHOP的会员扩展信息列表
	 *
	 * @return string[]
	 */
	function getMemberExtendList(){
		$this->setShopPrefix();
		$member_extend_array = $GLOBALS['db']->GetList('',$page,'member_extend');
		return $member_extend_array;
	}

	/**
	 * 得到DEDECMS的会员信息列表
	 *
	 */
	function dedecmsMemberList(){
		if ($this->setConnect()){
			$this->setPrefix();
			$sql = "select userid,pwd,uname as login_name,email from `".$this->_dbprefix."member`";
			$member_array = $this->GetArray($sql);
			$this->changeConnectBack();
			return $member_array;
		}else{
			return false;
		}
	}

	/**
	 * 得到DISCUZ的会员信息列表
	 *
	 */
	function discuzMemberList(){
		if ($this->setConnect()){
			$this->setPrefix();
			$sql = "select uid as userid,password as pwd,username as login_name,email from `".$this->_dbprefix."members`";
			$member_array = $this->GetArray($sql);
			$this->changeConnectBack();
			return $member_array;
		}else{
			return false;
		}
	}
		
	/**
	 * 得到PHPCMS的会员信息列表
	 *
	 */
	function phpcmsMemberList(){
		if ($this->setConnect()){
			$this->setPrefix();
			$sql = "select userid,password as pwd,username as login_name,email from `".$this->_dbprefix."member`";
			$member_array = $this->GetArray($sql);
			$this->changeConnectBack();
			return $member_array;
		}else{
			return false;
		}
	}

	/**
	 * 得到phpwind的会员信息列表
	 *
	 */
	function phpwindMemberList(){
		if ($this->setConnect()){
			$this->setPrefix();
			$sql = "select uid as userid,password as pwd,username as login_name,email from `".$this->_dbprefix."members`";
			$member_array = $this->GetArray($sql);
			$this->changeConnectBack();
			return $member_array;
		}else{
			return false;
		}
	}

	/**
	 * 得到帝国的会员信息列表
	 *
	 */
	function ecmsMemberList(){
		if ($this->setConnect()){
			$this->setPrefix();
			$sql = "select userid,password as pwd,username as login_name,email from `".$this->_dbprefix."enewsmember`";
			$member_array = $this->GetArray($sql);
			$this->changeConnectBack();
			return $member_array;
		}else{
			return false;
		}
	}

	/**
	 * 数据转换
	 *
	 * @param 整合目标对象字符串描述 $type
	 */
	function implodeToShop($type){
		//链接商城数据库
//		$this->changeConnectBack();
//		$shop_member        = $this->getMemberList();//会员信息
//		$shop_member_extend = $this->getMemberExtendList();//会员扩展信息
		//链接需要整合系统的数据库
		$this->setConnect();
		//取会员信息
		$function_name      = $type . "MemberList";
		$other_member_list  = $this->$function_name();
		if ($other_member_list == false){
			return false;
		}
		$new_array = array();
		for($i=$start;$i<($start + $count);$i++){
			if($other_member_list[$i]['login_name'] != ''){
				$new_array[] = $other_member_list[$i];
			}
		}
		return $this->copyToShop($other_member_list);
	}

	/**
	 * 清空shop数据库表member和member_extend的数据
	 *
	 * @param bool
	 */
	function truncateShop(){
		//链接商城数据库
		$this->changeConnectBack();
		$sql = "TRUNCATE TABLE `" . $GLOBALS['dbconfig']['dbprefix'] . "member`";
		$GLOBALS['db']->Execute($sql);
		$sql = "TRUNCATE TABLE `" . $GLOBALS['dbconfig']['dbprefix'] . "member_extend`";
		if(!$GLOBALS['db']->Execute($sql)){
			return false;
		}
		else {
			return true;
		}
	}

	/**
	 * 将整合系统的会员插入商城系统中
	 *
	 * @param string[] $other_member_list
	 * @return boolen
	 */
	function copyToShop($other_member_list){
		//链接商城数据库
		$this->changeConnectBack();
		
		/**
		 * 创建会员对象
		 */
		if (!is_object($obj_member)){
			require_once ("member.class.php");
			$obj_member = new MemberClass();
		}
		
		foreach ($other_member_list as $v) {
//			if (function_exists('iconv') && ICONV_VERSION < 2){
//				$member = iconv($this->charset,"utf-8",$v['login_name']);
//			}else{
//				$member = $v['login_name'];
//			}
			$value_array = array();
//			$value_array['login_name'] = $member;
			$value_array['login_name'] = $v['login_name'];
			$value_array['password'] = $v['pwd'];
			$value_array['email'] = $v['email'];
			$value_array['regist_time'] = time();
			$insert_rs = $GLOBALS['db']->InsertRow($value_array, 'member', 'member_id');
			if ($insert_rs !== true) {
				$lost_str .= $value_array['login_name'] . "\r\n";
			}else {
				//得到刚才添加的会员ID
				$member_row = $GLOBALS['db']->GetLastRow("member","member_id");
				//在会员扩展表中插入一条记录，会员ID要与刚添加的会员ID一致
				$insert_rs = $GLOBALS['db']->InsertRow(array('member_id'=>$member_row['member_id']), 'member_extend', 'extend_id');
				if ($insert_rs !== true) {
					$lost_str .= $member_row['login_name'] . "\r\n";
				}else {
					//更新用户组
					$obj_member->updateMemberToGroup(intval($member_row['member_id']));
				}
			}
		}
		if (!empty($lost_str)) {
			return $lost_str;
		}else {
			return true;
		}
	}
	
	/**
	 * 验证需要整合的数据库是否可以连接并且有该库
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function connectConformityDB(){
		//初始化语言包
		$this->getlang("sys_common");
		if (!($conn = @mysql_connect( $this->inte_db_server_name, $this->inte_db_account, $this->inte_db_password ))) {
			@mysql_close($conn);
			return $this->_lang['errSysConnectDatabase'];
		}else{
			if (!mysql_select_db($this->inte_db_name,$conn)) {
				@mysql_close($conn);
				return $this->_lang['errSysSelDatabase'];
			}else {
				$this->_conn = $conn;
				$this->_conn_write = $conn;
				return true;
			}
		}
	}
	
	/**
	 * 取sql信息
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array/string
	 */
	function GetArray($sql){
		if(strtolower($GLOBALS['dbconfig']['database_charset']) == 'gbk'){
			$charset_string = $this->gbk_string;
		}else{
			$charset_string = $this->utf_string;
		}
		@mysql_query($charset_string);
		
		if ($this->_conn == ''){
			$result = @mysql_query($sql);
		}else{
			$result = @mysql_query($sql,$this->_conn);
		}
		if (!$result) {
			return false;
		}
		$array = array();
		while($row = $GLOBALS['db']->fetch_array($result)){
			$array[] = $row;
		}
		return (array)$array;
	}
}
?>